package com.campus.service.Impl;

import com.campus.dao.SchoolDao;
import com.campus.pojo.SysArea;
import com.campus.service.SchoolService;
import com.campus.tools.Tree;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {
    @Autowired
    SchoolDao dao;

    @Override
    public List<Tree> getSchoolTree() {

        List<SysArea> areaList = dao.getParentAreaList();//获取所有省
        List<Tree> provinceTreeList = new ArrayList<>();
        //将区域 转换成 树节点
        for (SysArea area : areaList) {
            Tree treeNode = new Tree();
            treeNode.setId(area.getAreaNumber());
            treeNode.setName(area.getName());
            treeNode.setOpen(true);
            treeNode.setChecked(false);
            treeNode.setParent(false);
            treeNode.setNocheck(false);
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("isLeaf",area.getIsLeaf());
            treeNode.setAttributes(jsonObject.toString());
            provinceTreeList.add(treeNode);//加入到树
        }

        //遍历树，设置孩子节点
        for (Tree parentTreeNode : provinceTreeList){
            String areaNumber = parentTreeNode.getId();//获取父节点的areaNumber
            List<SysArea> childrenAreaList = dao.getChildrenAreaList(areaNumber);//根据父节点areaNumber获取子节点列表  市！！！
            List<Tree> tree = new ArrayList<>();
            //将区域 转换成 树节点
            for (SysArea area : childrenAreaList){
                Tree treeNode = new Tree();
                treeNode.setId(area.getAreaNumber());
                treeNode.setName(area.getName());
                treeNode.setOpen(true);
                treeNode.setChecked(false);
                treeNode.setParent(false);
                treeNode.setNocheck(false);
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("isLeaf",area.getIsLeaf());
                treeNode.setAttributes(jsonObject.toString());
                treeNode.setChildren(getChildrenList(treeNode.getId()));
                tree.add(treeNode);
            }
            parentTreeNode.setChildren(tree);
        }

        //建立一个根节点
        Tree root = new Tree();
        root.setName("中国");
        root.setId(null);
        root.setOpen(true);
        root.setAttributes("{\"isLeaf\":\"0\"}");
        root.setChildren(provinceTreeList);

        List<Tree> result = new ArrayList<>();
        result.add(root);
        return result;
    }

    //获取childrenList
    private List<Tree> getChildrenList(String parentId){
        List<SysArea> areaList = dao.getChildrenAreaList(parentId);
        List<Tree> tree = new ArrayList<>();
        for (SysArea area : areaList){
            Tree treeNode = new Tree();
            treeNode.setId(area.getAreaNumber());
            treeNode.setName(area.getName());
            treeNode.setOpen(true);
            treeNode.setChecked(false);
            treeNode.setParent(false);
            treeNode.setNocheck(false);
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("isLeaf",area.getIsLeaf());
            treeNode.setAttributes(jsonObject.toString());
            treeNode.setChildren(getChildrenList(area.getAreaNumber()));//递归 !!!
            tree.add(treeNode);//加入到树
        }
        return tree;
    }

    @Override
    public SysArea getSelectedArea(String areaNumber) {
        return dao.getSelectedArea(areaNumber);
    }

    @Override
    public String selectParentName(String areaNumber) {
        return dao.selectParentName(areaNumber);
    }

    @Override
    public int isRepeatName(SysArea area) {
        return dao.isRepeatName(area);
    }

    @Override
    public int updateSchool(SysArea sysArea) {
        return dao.updateSchool(sysArea);
    }

    @Override
    public int isRepeatAreaNumber(SysArea area) {
        return dao.isRepeatAreaNumber(area);
    }

    @Override
    public int addSchool(SysArea area) {
        return dao.addSchool(area);
    }

    @Override
    public int delSchool(String ids) {
        return dao.delSchool(ids);
    }
}
